const http = require('http')
const fs = require('fs')
const path = require('path')

const server = http.createServer()

server.listen(9999, () => {
  console.log('服务器开启了：点击访问http://localhost:9999')
})

server.on('request', (req, res) => {
  if (req.url === '/') {
    req.url = '/index.html'
  }
  const filePath = path.join(__dirname, 'www', 'demo-echarts', decodeURI(req.url))
  fs.readFile(filePath, (err, data) => {
    if (err) {
      fs.readFile(path.join(__dirname, 'www', 'demo-echarts', '404.html'), (err, data) => {
        res.statusCode = 404
        res.end(data)
      })
      return
    }
    // 读取成功
    res.end(data)
  })
})